<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% content_for :page_title do %>{{#t}}Merge User Accounts{{/t}}<% end %>
<% content_for :stylesheets do %>
<style>
.merge_results {
  border-collapse: collapse;
  margin: 10px 0;
}
.merge_results td, .merge_results th {
  padding-right: 10px;
  padding-bottom: 3px;
  vertical-align: top;
}
.merge_results .result > td, .merge_results .result > th {
  padding: 5px;
}
.merge_results tbody .result > td, .merge_results tbody .result > th {
  border-top: 1px solid #ddd;
}
.merge_results .result > td {
  border-left: 1px solid #ddd;
}
.merge_results thead .result > td {
  font-weight: bold;
}
.merge_results th {
  font-weight: bold;
  padding-right: 10px;
}
</style>
<% end %>
<div style="margin-top: 10px;">
  <% if @other_user %>
    <h1>{{#t}}Really Merge User Accounts?{{/t}}</h1>
    <p><strong>
      {{#t}}This process cannot be undone, so please make sure you're certain before you continue.{{/t}}
    </strong></p>
    <p>
      <%= t('merge_result_explanation', "Are you sure you want to merge the account, %{source_user_name}
      (%{source_user_email}) into this account, %{target_user_name} (%{target_user_email})? This
      process will have the following end result:",
            :source_user_name => @user.name, :source_user_email => @user.email,
            :target_user_name => @other_user.name, :target_user_email => @other_user.email) %>
    {{> views/merge_results}}
    </p>
    <%= form_tag user_merge_path(@user, :new_user_id => @other_user.id), :method => :post do %>
      <div class="button-container">
        <button type="submit" class="btn">{{#t}}Merge User Accounts{{/t}}</button>
        <a href="<%= dashboard_path %>" class="btn button-secondary">{{#t}}Cancel{{/t}}</a>
      </div>
    <% end %>
  <% else %>
    <h1>{{#t}}Merge User Accounts{{/t}}</h1>
    <% if @pending_other_user %>
      <%= t('merge_result_explanation_again', "You've selected to merge the user, %{user_name} (%{user_email}) into
      the account, %{pending_other_user_name} (%{pending_other_user_email}).
      This process will have the following end result:",
            :user_name => @user.name, :user_email => @user.email,
            :pending_other_user_name => @pending_other_user.name, :pending_other_user_email => @pending_other_user.email) %>
      {{> views/merge_results}}
      <a class="btn" id="prepare_to_merge" style="width: 6em;" href="<%= user_admin_merge_url(@user.id, :new_user_id => @pending_other_user.id) %>">{{#t}}Prepare to Merge Users{{/t}}</a>
      <span style="font-size: 0.8em; padding-left: 50px;">
        <a class="btn" id="switch_user_positions" style="width: 6em;" href="<%= user_admin_merge_url(@pending_other_user.id, :pending_user_id => @user.id) %>">{{#t}}Switch User Positions{{/t}}</a>
        <a class="btn merge_with_other" style="width: 13em;" href="<%= user_admin_merge_url(@user.id, :clear => 1) %>"><%= t('merge_somone_else', 'Merge Someone Else With %{user_name}', :user_name => @user.name) %></a>
        <a class="btn merge_with_other" style="width: 13em;" href="<%= user_admin_merge_url(@pending_other_user.id, :clear => 1) %>"><%= t('merge_somone_else', 'Merge Someone Else With %{user_name}', :user_name => @pending_other_user.name) %></a>
      </span>
    <% else %>
      <p><%= t('merge_user_initial_instructions', "You've selected to merge the user, %{user_name} (%{user_email}) with another account.
      You can search for the user you'd like to merge with this user using the form below.", :user_name => @user.name, :user_email => @user.email) %></p>
      <table>
        <tr>
          <td style="vertical-align: top; padding: 10px 30px 10px;">
            <h2>{{#t}}Find a User{{/t}}</h2>
            <% accounts = @current_user.associated_accounts.sort_by(&:name).uniq.select { |a| a.grants_any_right?(@current_user, session, :manage_user_logins, :read_roster) } %>
            <% if accounts.empty? %>
              {{#t}}You don't have permission to search any accounts{{/t}}
            <% else %>
              <select id="account_select" style="<%= hidden unless accounts.length > 1%>">
                <% accounts.each do |account| %>
                  <option value="<%= account.id %>"><%= account.name %></option>
                <% end %>
              </select>
              <div style="margin-top: 10px; margin-bottom: 20px;">
                <% accounts.each do |account| %>
                  <div id="account_search_<%= account.id %>" class="account_search" style="display: none;">
                    <label for="user_name_<%= account.id %>"><%= before_label('name', 'Name') %></label>
                    <input id="user_name_<%= account.id %>" data-autocomplete-source="<%= account_users_path(account) + '.json' %>" class="user_name" />
                  </div>
                <% end %>
              </div>
              <div style="margin-top: 20px;">
                <span id="selected_name" style="font-weight: bold; padding-right: 10px;"></span>
                <a href="#" style="display: none;" rel="<%= user_admin_merge_url(@user.id, :pending_user_id => "{{ id }}") %>" id="select_name" type='button' class='btn btn-small select_button'><%= t 'links.select', "Select" %></a>
              </div>
            <% end %>
          </td>
          <td style="vertical-align: top; padding: 10px 10px 10px 30px;">
            <h2>{{#t}}Or Enter a User ID{{/t}}</h2>
            <%= form_tag user_admin_merge_url(@user.id), {:method => :get} do %>
              <label for="manual_user_id"><%= before_label('user_id', 'User ID') %></label>
              <input id="manual_user_id" name="pending_user_id" type="text" style="width: 50px;"/>
              <button type="submit" class="btn btn-small">{{#t}}Go{{/t}}</button>
            <% end %>
          </td>
        </tr>
      </table>
      <% js_bundle 'legacy/users_admin_merge' %>
    <% end %>
  <% end %>
</div>
